package com.crois.barrier.admin.controller;


import com.crois.barrier.admin.dto.PluginDataDto;
import com.crois.barrier.admin.entity.PluginData;
import com.crois.barrier.admin.mapstruct.PluginDataDtoEntityMapper;
import com.crois.barrier.admin.resp.BarrierResponseWrapper;
import com.crois.barrier.admin.service.IPluginDataSvc;
import com.crois.barrier.common.utils.UtilsAndCommons;
import com.crois.barrier.common.response.BarrierResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 15:41 2021/10/27
 */
@RequestMapping(UtilsAndCommons.DEFAULT_BARRIER_ADMIN_PLUGIN_CONTEXT)
@RestController
public class PluginCtl {

    @Autowired
    @Qualifier("pluginDataSvc")
    private IPluginDataSvc pluginDataSvc;

    /***
     * barrier delete route plugin
     */
    @DeleteMapping("/route/{routeId}/{pluginId}")
    public BarrierResponseEntity<?> routePluginDelete(@PathVariable("routeId") Integer routeId,@PathVariable("pluginId") Integer pluginId){
        pluginDataSvc.deleteRoutePlugin(routeId,pluginId);
        return BarrierResponseWrapper.ok();
    }

    /***
     * barrier save route plugin
     */
    @PostMapping("/route/{routeId}")
    public BarrierResponseEntity<?> routePluginAdd(@PathVariable("routeId") Integer routeId,@RequestBody PluginDataDto pluginDataDto){
        pluginDataSvc.saveRoutePlugin(routeId,pluginDataDto.getId());
        return BarrierResponseWrapper.ok();
    }



    /***
     * barrier route plugin list
     */
    @GetMapping("/route/{routeId}/"+UtilsAndCommons.BARRIER_LIST_CONTEXT)
    public BarrierResponseEntity<List<?>> routePluginList(@PathVariable("routeId") Integer routeId){
        List<PluginData> pluginDataList = pluginDataSvc.findRoutePluginList(routeId);
        return BarrierResponseWrapper.okList(PluginDataDtoEntityMapper.INSTANCE.toPluginDataDtoList(pluginDataList));
    }

    /***
     * barrier plugin list
     */
    @GetMapping(UtilsAndCommons.BARRIER_LIST_CONTEXT)
    public BarrierResponseEntity<List<?>> pluginList(@RequestParam(value = "pluginName",required = false) String pluginName){
        List<PluginData> pluginDataList = pluginDataSvc.findPluginList(pluginName);
        return BarrierResponseWrapper.okList(PluginDataDtoEntityMapper.INSTANCE.toPluginDataDtoList(pluginDataList));
    }


    /***
     * barrier plugin detail
     */
    @GetMapping(UtilsAndCommons.BARRIER_DETAIL_CONTEXT+"/{pluginId}")
    public BarrierResponseEntity<?> pluginDetail(@PathVariable("pluginId") Integer pluginId){
        PluginData pluginData = pluginDataSvc.getById(pluginId);
        return BarrierResponseWrapper.ok(PluginDataDtoEntityMapper.INSTANCE.toPluginDataDto(pluginData));
    }

    /***
     * barrier plugin save
     */
    @PostMapping(UtilsAndCommons.BARRIER_ADD_CONTEXT)
    public BarrierResponseEntity<Void> pluginAdd(@RequestBody PluginDataDto pluginDataDto){
        PluginData pluginData = PluginDataDtoEntityMapper.INSTANCE.toPluginDataEntity(pluginDataDto);
        pluginDataSvc.savePlugin(pluginData);
        return BarrierResponseWrapper.ok();
    }

    /***
     * barrier plugin update
     */
    @PutMapping(UtilsAndCommons.BARRIER_UPDATE_CONTEXT+"/{pluginId}")
    public BarrierResponseEntity<Void> apiRouteGroupUpdate(@PathVariable("pluginId") Integer pluginId,@RequestBody PluginDataDto pluginDataDto){
        pluginDataDto.setId(pluginId);
        pluginDataSvc.updatePlugin(PluginDataDtoEntityMapper.INSTANCE.toPluginDataEntity(pluginDataDto));
        return BarrierResponseWrapper.ok();
    }

    /***
     * barrier plugin delete
     */
    @DeleteMapping(UtilsAndCommons.BARRIER_DELETE_CONTEXT+"/{pluginId}")
    public BarrierResponseEntity<Void> apiRouteGroupDelete(@PathVariable("pluginId") Integer pluginId){
        pluginDataSvc.removePlugin(pluginId);
        return BarrierResponseWrapper.ok();
    }


}
